﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Dispatcher.ViewModels;
using Repository;

namespace Dispatcher.Views
{
    /// <summary>
    /// Interaction logic for AdminToolsView.xaml
    /// </summary>
    public partial class AdminToolsView
    {
        public AdminToolsView()
        {
            InitializeComponent();
        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = (EntityListViewModel<User>) DataContext;
            var users = (ObservableCollection<User>) viewModel.Items;
            var userEditViewModel = new UserEditViewModel(MainWindow.Model.ModelContainer);
            var dialog = new UserEditView {DataContext = userEditViewModel};
            if (dialog.ShowDialog() == true)
            {
                var user = new User();
                user.Name = userEditViewModel.Name;
                user.PasswordHash = userEditViewModel.PasswordHash;
                user.Person = userEditViewModel.SelectedPerson;
                user.Role = userEditViewModel.SelectedRole;
                users.Add(user);
            }
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = (EntityListViewModel<User>)DataContext;
            var users = (ObservableCollection<User>)viewModel.Items;
            var userEditViewModel = new UserEditViewModel(MainWindow.Model.ModelContainer);
            var user = (User)ListView.ItemsDataGrid.SelectedCells.First().Item;

            userEditViewModel.Name = user.Name;
            userEditViewModel.PasswordHash = user.PasswordHash;
            userEditViewModel.SelectedPerson = user.Person;
            userEditViewModel.SelectedRole = user.Role;

            var dialog = new UserEditView { DataContext = userEditViewModel };
            if (dialog.ShowDialog() == true)
            {
                user.Name = userEditViewModel.Name;
                user.PasswordHash = userEditViewModel.PasswordHash;
                user.Person = userEditViewModel.SelectedPerson;
                user.Role = userEditViewModel.SelectedRole;
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            ApplicationCommands.Delete.Execute(null, ListView);
            return;
            var viewModel = (EntityListViewModel<User>)DataContext;
            var users = (ObservableCollection<User>)viewModel.Items;
            var user = (User)ListView.ItemsDataGrid.SelectedCells.First().Item;
            MainWindow.Model.ModelContainer.UserSet.DeleteObject(user);
        }
    }
}
